<?php
/**
 *  View that uses templates
 *
 */
class Yz_View_Template
    extends Yz_View_Default
    implements Yz_View_Interface
{

    const NO_CONTENT = '<!-- No template -->';

    /**
     *  @var string
     */
    protected $newLocation;
    /**
     *  @var Yz_Template_Interface
     */
    protected $template;

    /**
     *  @var string
     */
    protected $text;


    /****************************
     *      Public methods      *
     ****************************/


    /** get the text (string) content of the view
     *  @return string
     */
    public function getContent()
    {
        $t =& $this->text;
        if (null === $t) {
            if (null !== $this->template) {
                $t = $this->template->render();
            } else {
                $t = self::NO_CONTENT;
            }
        }
        return $t;
    }

    /**
     *
     */
    public function setTemplate( Yz_Template_Interface $template )
    {
        debug($template->className());
        $this->template = $template;
        return $this;
    }


    public function render(Yz_HTTP_Response_Interface $rs)
    {
        debug();
        if (null !== $this->newLocation) {
            $rs
                ->setStatus($rs::STATUS_SEE_OTHER)
                ->setLocation($this->newLocation)
            ;
        }
        return parent::render($rs);
    }


    public function setRedirect($location)
    {
        debug();
        $this->newLocation = null === $location
            ? null
            : Yz_String::test($location);
        return $this;
    }

} // class
